﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_RegisterCardEdit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadAction();
            LoadGoodType();
            BindData();
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (tbGoodName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入商品名称。");
            return;
        }

        //if (tbPrice.Text.Trim() == "")
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入商品价格。");
        //    return;
        //}
        //else
        //{
        //    if (ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(),-1) <= 0)
        //    {
        //        ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的商品价格。");
        //        return;
        //    }
        //}

        //if (ddlGoodType.SelectedValue == "" || ddlGoodType.SelectedValue == "0")
        if (ddlGoodType.Text == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请选择商品类别。");
            return;
        }


        string fileName = "";
        //string url = "~/UpFiles/";
        //if (ShoveEL._IO.File.UploadFile(this.Page, this.File1, url, ref fileName, false) < 0)
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, "文件上传失败");
        //}  


        if (hID.Value != "")
        {
            try
            {
                int i = DAL.Tables.T_Goods.Update("[ID]=" + hID.Value.Trim(), tbGoodName.Text.Trim(), true, 0, false, tbGoodNorm.Text.Trim(), true, tbGoodUnit.Text.Trim(), true, tbGoodColor.Text.Trim(), true, tbRemark.Text.Trim(), true, fileName, true, ShoveEL._Convert.StrToLong(ddlGoodType.SelectedValue, 0), true, tbGoodNo.Text.Trim(), true, tbGoodPassword.Text.Trim(), true, 0, false);

                if (i >= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "修改成功!");
                }
            }
            catch { }
        }
        else
        {
            try
            {
                int i = DAL.Tables.T_Goods.Insert(tbGoodName.Text.Trim(), true, 0, false, tbGoodNorm.Text.Trim(), true, tbGoodUnit.Text.Trim(), true, tbGoodColor.Text.Trim(), true, tbRemark.Text.Trim(), true, fileName, true, ShoveEL._Convert.StrToLong(ddlGoodType.SelectedValue, 0), true, tbGoodNo.Text.Trim(), true, tbGoodPassword.Text.Trim(), true, 0, false);

                if (i >= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "添加成功!");

                    tbGoodName.Text = "";
                    tbGoodNo.Text = "";
                    tbGoodNorm.Text = "";
                    tbGoodColor.Text = "";
                    tbGoodPassword.Text = "";
                    tbGoodUnit.Text = "";
                    tbRemark.Text = "";
                }
            }
            catch { }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("RegisterCardManager.aspx");
    }

    private void ReadAction()
    {
        this.hID.Value = ShoveEL._Web.Utility.GetRequest("id");
    }

    private void BindData()
    {
        if (this.hID.Value != "")
        {
            long ID = ShoveEL._Convert.StrToLong(hID.Value, -1);
            DataTable dt = null;

            try
            {
                dt = DAL.Tables.T_Goods.Select("", "[ID]=" + ID, "");

                if (dt == null)
                {
                    PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);
                    return;
                }

                tbGoodName.Text = dt.Rows[0]["GoodName"].ToString();
                tbGoodNorm.Text = dt.Rows[0]["GoodNorm"].ToString();
                tbGoodColor.Text = dt.Rows[0]["GoodColor"].ToString();
                tbGoodUnit.Text = dt.Rows[0]["GoodUnit"].ToString();
                //tbPrice.Text = dt.Rows[0]["Price"].ToString();
                tbRemark.Text = dt.Rows[0]["Remark"].ToString();
                tbGoodNo.Text = dt.Rows[0]["GoodNo"].ToString();
                tbGoodPassword.Text = dt.Rows[0]["Password"].ToString();
                string fileName = dt.Rows[0]["GoodPic"].ToString();

                string url = "~/UpFiles/" + fileName;

                //Image1.ImageUrl = url;

            }
            catch { }
        }
    }

    private void LoadGoodType()
    {
        DataTable dt = null;

        try
        {
            dt = DAL.Tables.T_GoodType.Select("ID,TypeName", "[ID] = 1", "");

            if (dt != null && dt.Rows.Count > 0)
            {
                this.ddlGoodType.DataSource = dt;
                this.ddlGoodType.DataTextField = "TypeName";
                this.ddlGoodType.DataValueField = "ID";

                this.ddlGoodType.DataBind();
            }
        }
        catch { }
    }
}
